function MyUtils() {
};

MyUtils.removeChildElements = function(elem) {
  while (elem && elem.firstChild) {
    elem.removeChild(elem.firstChild);
  }
};

MyUtils.removeTableRows = function(table, rowsToKeep) {
  while (table.rows.length > rowsToKeep) {
    table.deleteRow(table.rows.length - 1);
  }
};

MyUtils.findPos = function(elem) {
  var curleft = 0, curtop = 0;
  if (elem.offsetParent) {
    curleft = elem.offsetLeft;
    curtop = elem.offsetTop;
    while ((elem = elem.offsetParent)) {
      curleft += elem.offsetLeft;
      curtop += elem.offsetTop;
    }
  }
  return [ curleft, curtop ];
};

MyUtils.getUnique = function() {
  var chars = [];
  for (var i = 48, il = 58; i < il; i++) {
    chars[chars.length] = String.fromCharCode(i);
  }
  for (var i = 65, il = 91; i < il; i++) {
    chars[chars.length] = String.fromCharCode(i);
  }
  for (var i = 97, il = 123; i < il; i++) {
    chars[chars.length] = String.fromCharCode(i);
  }
  var unique = '';
  for (var i = 0, il = 16; i < il; i++) {
    unique += chars[parseInt(Math.abs((Math.random() * 100) % chars.length), 10)];
  }
  return unique;
};

MyUtils.cleanDatepicker = function() {
  var old_fn = $.datepicker._updateDatepicker;
  $.datepicker._updateDatepicker = function(inst) {
    old_fn.call(this, inst);
    var buttonPane = $(this).datepicker("widget").find(".ui-datepicker-buttonpane");
    $("<button type='button' class='ui-datepicker-clean ui-state-default ui-priority-primary ui-corner-all'>Clear</button>").appendTo(buttonPane).click(function(ev) {
      $.datepicker._clearDate(inst.input);
    }) ;
  }
};

MyUtils.getShort = function(str, len) {
  if (str.length <= len) {
    return str;
  }
  
  var left = str.substr(0, len);
  return left + '...';
};

MyUtils.setMatchedText = function(elm, string, search, length) {
  var idx, elm, PRE = '...', POST = '...';
  if (string == null || search == null) {
    return;
  }
  var pane;
  elm.title = string;
  if (string.toLowerCase().indexOf(search.toLowerCase()) != -1 && search.length > length) {
    elm = document.createElement('span');
    elm.style.backgroundColor = '#ffff00';
    elm.appendChild(document.createTextNode(search.substr(0, length) + POST));
    elm.appendChild(elm);
    return;
  } else if (string.toLowerCase().indexOf(search.toLowerCase()) == -1 && string.length > length) {
    string = string.substr(start, length) + POST;
    elm.appendChild(document.createTextNode(string));
    return;
  } else if (string.toLowerCase().indexOf(search.toLowerCase()) != -1 && string.length > length) {
    var start = string.toLowerCase().indexOf(search.toLowerCase());
    var space = (length - search.length) / 2;
    if (start < space) {
      start = 0;
      PRE = '';
    } else {
      start = start - space;
    }
    string = PRE + string.substr(start, length) + POST;
  }
  while (search != '' && string.toLowerCase().indexOf(search.toLowerCase()) != -1) {
    idx = string.toLowerCase().indexOf(search.toLowerCase());
    elm.appendChild(document.createTextNode(string.substr(0, idx)));
    elm = document.createElement('span');
    elm.style.backgroundColor = '#ffff00';
    elm.appendChild(document.createTextNode(string.substr(idx, search.length)));
    elm.appendChild(elm);
    string = string.substr(idx + search.length);
  }
  elm.appendChild(document.createTextNode(string));
};

MyUtils.daysInMonth = function(month, year) {
  return new Date(year, month, 0).getDate();
}
